package cn.edu.scau.cmi.ema.service;

import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;


import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.Session;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;
import cn.edu.scau.cmi.ema.service.base.CmiEmaService;

import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.persistence.Query;
import java.util.Iterator;
@Service("EventqualitytypeService")
@Transactional

public class EventqualitytypeServiceImpl extends CmiEmaService implements EventqualitytypeService {
	public EventqualitytypeServiceImpl() {
	}

	@Transactional
	public Eventqualitytype saveEventqualitytype(Eventqualitytype eventqualitytype) {

		if (eventqualitytype.getId() == null) {
			Event event = eventDAO.findEventById(eventqualitytype.getEvent().getId());
			Qualitytype qualitytype = qualitytypeDAO.findQualitytypeById(eventqualitytype.getQualitytype().getId());

			Eventqualitytype existingEventqualitytype = eventqualitytypeDAO.findEventqualitytypeByPhysicalPrimaryKey(
					event.getId(), qualitytype.getId(), eventqualitytype.getDegree());

			eventqualitytype.setEvent(event);
			eventqualitytype.setQualitytype(qualitytype);

			if (isEquals(eventqualitytype, existingEventqualitytype)) {
				return existingEventqualitytype;
			}

			eventqualitytype = eventqualitytypeDAO.store(eventqualitytype);
			eventqualitytypeDAO.flush();

		} else {
			Eventqualitytype existingEventqualitytype = eventqualitytypeDAO
					.findEventqualitytypeByPrimaryKey(eventqualitytype.getId());

			eventqualitytype = eventqualitytypeDAO.store(eventqualitytype);
			eventqualitytypeDAO.flush();
		}

		return eventqualitytype;
	}

	public void deleteEventqualitytype(Eventqualitytype eventqualitytype) {
		eventqualitytypeDAO.remove(eventqualitytype);
		eventqualitytypeDAO.flush();
	}

	@Transactional
	public void deleteEventqualitytypes(Set<Integer> deleteEventqualitytypesId) {
		Query query = eventqualitytypeDAO.createNamedQuery("deleteEventqualitytypesById", -1, -1);
		query.setParameter(1, deleteEventqualitytypesId);
		query.executeUpdate();
	}

	@Transactional
	public Set<Eventqualitytype> loadEventqualitytypes() {
		return eventqualitytypeDAO.findAllEventqualitytypes();

	}

	@Transactional
	public Set<Eventqualitytype> loadReferenceEventqualitytypes(Set<Eventqualitytype> eventqualitytypes) {
		Iterator<Eventqualitytype> eventqualitytypesIterator = eventqualitytypes.iterator();
		Set<Eventqualitytype> referencedEventqualitytypes = new HashSet<>();
		while (eventqualitytypesIterator.hasNext()) {
			Eventqualitytype currentEventqualitytype = eventqualitytypesIterator.next();

		}
		return referencedEventqualitytypes;
	}

	public Set<Eventqualitytype> loadEventqualitytypesByIdSet(Set<Integer> eventqualitytypeIdSet) {

		Iterator<Integer> eventqualitytypeIdIterator = eventqualitytypeIdSet.iterator();
		Set<Eventqualitytype> eventqualitytypeSet = new HashSet<>();
		while (eventqualitytypeIdIterator.hasNext()) {
			Integer id = eventqualitytypeIdIterator.next();
			Eventqualitytype currentEventqualitytype = eventqualitytypeDAO.findEventqualitytypeById(id);
			eventqualitytypeSet.add(currentEventqualitytype);
		}
		return eventqualitytypeSet;
	}

	@Transactional
	public List<Eventqualitytype> findAllEventqualitytypes(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Eventqualitytype>(
				eventqualitytypeDAO.findAllEventqualitytypes(startResult, maxRows));
	}

	@Transactional
	public Eventqualitytype findEventqualitytypeByPrimaryKey(Integer id) {
		return eventqualitytypeDAO.findEventqualitytypeByPrimaryKey(id);
	}

	@Transactional
	public Integer countEventqualitytypes() {
		return ((Long) eventqualitytypeDAO.createQuerySingleResult("select count(o) from Eventqualitytype o")
				.getSingleResult()).intValue();
	}

	@Transactional
	public Integer countEventqualitytypes(CmiPagination page) {
		String sql = CmiSqlUtil.createCountHql(page, Eventqualitytype.class);
		return ((Long) eventqualitytypeDAO.createQuerySingleResult(sql).getSingleResult()).intValue();
	}

	/**
	 * search for name with ajax
	 * 
	 */
	@Transactional
	public Integer countEventqualitytypesByName(String queryName) {
		Query query = eventqualitytypeDAO.createNamedQuery("countEventqualitytypesByName", -1, -1);
		query.setParameter(1, queryName);
		return new Long((long) query.getSingleResult()).intValue();
	}

	@Transactional
	public Integer countEventqualitytypesByDetachedCriteria(CmiPagination page) {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Eventqualitytype.class);
		Session session = entityManager.unwrap(Session.class);
		detachedCriteria.setProjection(Projections.rowCount());
		return new Long((long) detachedCriteria.getExecutableCriteria(session).uniqueResult()).intValue();
	}

	public Map<String, ?> loadEventqualitytypeObjectReferencedSet(Eventqualitytype eventqualitytype) {
		HashMap<String, Object> objectReferencedSetMap = new HashMap<>();
		if (eventqualitytype.getEvent() != null) {

			Set<Integer> eventIds = new HashSet<Integer>();
			eventIds.add(eventqualitytype.getEvent().getId());
			objectReferencedSetMap.put("eventIds", eventIds);
		}
		if (eventqualitytype.getQualitytype() != null) {

			Set<Integer> qualitytypeIds = new HashSet<Integer>();
			qualitytypeIds.add(eventqualitytype.getQualitytype().getId());
			objectReferencedSetMap.put("qualitytypeIds", qualitytypeIds);
		}

		return objectReferencedSetMap;
	}

	public Map<String, ?> loadEventqualitytypeClassReferencedSet(Eventqualitytype eventqualitytype) {
		HashMap<String, Object> classReferencedSetMap = new HashMap<>();
		classReferencedSetMap.put("events", eventDAO.findAllEvents());
		classReferencedSetMap.put("qualitytypes", qualitytypeDAO.findAllQualitytypes());

		return classReferencedSetMap;
	}

	public boolean isEquals(Eventqualitytype eventqualitytype, Object obj) {
		if (obj == eventqualitytype)
			return true;
		if (!(obj instanceof Eventqualitytype))
			return false;
		Eventqualitytype comparedEventqualitytype = (Eventqualitytype) obj;
		if (!CheckPropertyEqual(eventqualitytype.getEvent().getId(), comparedEventqualitytype.getEvent().getId())) {
			return false;
		}
		if (!CheckPropertyEqual(eventqualitytype.getQualitytype().getId(),
				comparedEventqualitytype.getQualitytype().getId())) {
			return false;
		}
		if (!CheckPropertyEqual(eventqualitytype.getDegree(), comparedEventqualitytype.getDegree())) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}
}

